/**
 * Copyright (c) 2006 Amgad S Youssef
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package org.eclipsejcde.jcbp.builder.checkers;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaCore;
import org.eclipsejcde.core.CheckerStatus;
import org.eclipsejcde.core.IChecker;
import org.eclipsejcde.core.ICheckerStatus;
import org.eclipsejcde.core.JCDECore;

/**
 * @author Amgad S Youssef
 *
 */
public class SuperShareablesCountChecker implements IChecker {

	@Override
	public ICheckerStatus[] check(IResource resource) throws CoreException {
		if (!(resource instanceof IFile)) {
			return new ICheckerStatus[] {};
		}
		if (!resource.getName().endsWith(".java")) {
			return new ICheckerStatus[] {};
		}
		IJavaElement javaElement = JavaCore.create(resource);
		if (!(javaElement instanceof ICompilationUnit)) {
			return new ICheckerStatus[] {};
		}
		ICompilationUnit compilationUnit = (ICompilationUnit) javaElement;
		if (!JCDECore.isShareable(compilationUnit)) {
			return new ICheckerStatus[] {};
		}
		int countOfSuperShareables = JCDECore.getCountOfSuperShareables(compilationUnit);
		if (countOfSuperShareables > 1) {
			ICheckerStatus checkerStatus = new CheckerStatus(IMarker.SEVERITY_WARNING, "Implementing more than one Shareable interface is not recommended");
			ICheckerStatus[] checkerStatuses = new ICheckerStatus[] { checkerStatus };
			return checkerStatuses;
		}
		return new ICheckerStatus[] {};
	}

}
